package Servlet;

import Bean.Student;
import Utility.Utility;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name = "select", value = "/select")
public class Select extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        String userID = request.getParameter("id");
        String sql = "select * from student where id=?";
        ResultSet rs = Utility.query(sql, userID);
        Student stu;
        try {
            rs.next();
            stu = new Student(rs.getString(1),rs.getString(2)
            ,rs.getInt(3),rs.getString(4),rs.getString(5));

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        request.setAttribute("s",stu);
        request.getRequestDispatcher("/update.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
